package com.mycompany.crm.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.mycompany.crm.base.BaseService;
import com.mycompany.crm.dao.CustomerContactMapper;
import com.mycompany.crm.query.CustomerContactQuery;
import com.mycompany.crm.service.CustomerContactService;
import com.mycompany.crm.utils.AssertUtil;
import com.mycompany.crm.vo.CustomerContact;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;

/**
 * @author jy
 * @version 1.0
 * @date 2022/4/24 17:33
 */
@Service
public class CustomerContactServiceImpl extends BaseService<CustomerContact,Integer> implements CustomerContactService {

    @Resource
    private CustomerContactMapper customerContactMapper;

    /**
     * 多条件分页查询交往记录
     *
     * @param customerContactQuery
     * @return
     */
    @Override
    public Map<String, Object> queryCustomerContactByParams(CustomerContactQuery customerContactQuery) {
        Map<String, Object> map = new HashMap<>();
        // 开启分页
        PageHelper.startPage(customerContactQuery.getPage(), customerContactQuery.getLimit());
        // 得到对应分页对象
        PageInfo<CustomerContact> pageInfo = new PageInfo<>(customerContactMapper.selectByParams(customerContactQuery));
        // 设置map对象
        map.put("code",0);
        map.put("msg","success");
        map.put("count",pageInfo.getTotal());
        // 设置分页好的列表
        map.put("data",pageInfo.getList());
        return map;
    }

    /**
     * 添加交往记录
     *
     * @param customerContact
     */
    @Override
    @Transactional(propagation = Propagation.REQUIRED,rollbackFor = Exception.class)
    public void addCustomerContact(CustomerContact customerContact) {
        customerContact.setContactTime(customerContact.getContactTime());
        customerContact.setIsValid(1);
        customerContact.setCreateDate(new Date());
        customerContact.setUpdateDate(new Date());
        /* 3. 执行添加操作，判断受影响的行数 */
        AssertUtil.isTrue(customerContactMapper.insertSelective(customerContact) < 1, "添加交往记录失败！");
    }


     /**
     * 修改交往记录
     *
     * @param customerContact
     */
    @Override
    @Transactional(propagation = Propagation.REQUIRED,rollbackFor = Exception.class)
    public void updateCustomerContact(CustomerContact customerContact) {
        //* 1. 参数校验 *//*
        AssertUtil.isTrue(null == customerContact.getId(), "待更新记录不存在！");
        // 通过客户ID查询交往记录
        CustomerContact customerContact1 = customerContactMapper.selectByPrimaryKey(customerContact.getId());
        // 判断交往记录是否存在
        AssertUtil.isTrue(null == customerContact1, "待更新记录不存在！");
        //* 2. 设置参数的默认值  *//*
        assert customerContact1 != null;
        customerContact1.setUpdateDate(new Date());
        customerContact1.setContactTime(customerContact.getContactTime());
        customerContact1.setAddress(customerContact.getAddress());
        customerContact1.setOverview(customerContact.getOverview());
        //* 3. 执行更新操作，判断受影响的行数 *//*
        AssertUtil.isTrue(customerContactMapper.updateByPrimaryKeySelective(customerContact1) < 1, "修改交往记录失败！");
    }

    /**
     * 删除交往记录
     *
     * @param id
     */
    @Override
    @Transactional(propagation = Propagation.REQUIRED,rollbackFor = Exception.class)
    public void deleteCustomerContact(Integer id) {
        // 判断id是否为空，数据是否存在
        AssertUtil.isTrue(null == id, "待删除记录不存在！");
        // 通过id查询交往记录
        CustomerContact customerContact = customerContactMapper.selectByPrimaryKey(id);
        AssertUtil.isTrue(null == customerContact, "待删除记录不存在！");
        // 设置状态为失效
        assert customerContact != null;
        customerContact.setIsValid(0);
        customerContact.setUpdateDate(new Date());
        // 执行删除（更新）操作，判断受影响的行数
        AssertUtil.isTrue(customerContactMapper.updateByPrimaryKeySelective(customerContact) < 1, "删除交往记录失败！");
    }
}
